
#include "Parallel.h"

namespace common {

Parallel::Parallel(int id,Point* k,Point* l,Point* m,Point* n):common::Constraint(id,1,PARALLEL){
	setK(k);
	setL(l);
	setM(m);
	setN(n);
}
ublasVector Parallel::getValue() throw(MyException){
	// (L-K)x(N-M)
	ublasVector v(1);
	v(0) = ((*getL())-(*getK()))^((*getN())-(*getM()));

	return v;
}
ublasMatrix Parallel::getJacobianOnPoint(Point* p) throw(MyException){

	ublasMatrix R(ublasMatrixZero(2,2));
	R(0,1) = 1.0;
	R(1,0) = -1.0;
	ublasMatrix mR(R*(-1));

	ublasMatrix mout(1,2);

	Vector nmC((*getN())-(*getM()));
	Vector lkC((*getL())-(*getK()));
	ublasVector nm(2); 	ublasVector lk(2);
	nm <<  nmC;
	lk << lkC;

	if(k==p){
		row(mout,0)= prod(mR,nm);
	}
	else if(l==p){
		row(mout,0)= prod(R,nm);
	}
	else if(m==p){
		row(mout,0)= prod(R,lk);
	}
	else if(n==p){
		row(mout,0) = prod(mR,lk);
	}

	return mout;
}
ublasMatrix Parallel::getHessian(Point* s, Point* r) throw(MyException){

	ublasMatrix out(ublasMatrixZero(2,2));

	ublasMatrix R(ublasMatrixZero(2,2));
	R(0,1) = 1.0;
	R(1,0) = -1.0;
	ublasMatrix mR(R*(-1));

	if(k==s && m==r){
		out = R;
	}else if(k==s && n==r){
		out = mR;
	}else if(l==s && m==r){
		out = mR;
	}else if(l==s && n==r){
		out = R;
	}else if(m==s && k==r){
		out = mR;
	}else if(m==s && l==r){
		out = R;
	}else if(n==s && k==r){
		out = R;
	}else if(n==s && l==r){
		out = mR;
	}else {
		return ublasMatrixZero(2,2);
	}

	return out;

}
bool Parallel::isHessian() const{
	return true;
}

Parallel::~Parallel(){

}

}
